home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / Kibitz / Print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-06  |  8.3 KB  |  311 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** MultiFinder-Aware Shell Application
  5. **
  6. ** File:        print.c
  7. ** Written by:  Eric Soldan
  8. ** Based on:    Code from Pete "Luke" Alexander.
  9. **
  10. ** Copyright © 1989-1992 Apple Computer, Inc.
  11. ** All rights reserved. */
  12.  
  13.  
  14.  
  15. /*****************************************************************************/
  16.  
  17.  
  18.  
  19. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  20. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  21. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26.  
  27. #ifndef __RESOURCES__
  28. #include <Resources.h>
  29. #endif
  30.  
  31.  
  32.  
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. pascal void            PrintIdleProc(void);
  38. short                gPrintPage;
  39. static DialogPtr    PrintingStatusDialog;
  40.  
  41.  
  42.  
  43. /*****************************************************************************/
  44.  
  45.  
  46.  
  47. /* This print-loop function is designed to be called under various situations.
  48. ** The big issue that it handles is finder printing.  If multiple documents
  49. ** are to be printed from the finder, the user should only see one job dialog
  50. ** for all the files.  (If a job dialog is shown for each file, how does the
  51. ** user know for which file the dialog is for?)  So, for situations where
  52. ** there is more than one file to be printed, call this code the first time
  53. ** with the firstJob boolean true.  Normally, the jobDlg boolean will also
  54. ** be true, except that under 7.0, you may be printing in the background.
  55. ** If this is the case, you don't want a job dialog for even the first file,
  56. ** and you should pass in false for the jobDlg boolean in this case.  For
  57. ** files 2-N, you should pass false for both booleans.  For regular application
  58. ** printing, you should pass true for both booleans, since the file is the
  59. ** first (only) file, and you are not in the background.
  60. **
  61. ** After calling this function to print a document, you need to call it
  62. ** again with a nil document handle.  The print record for the first (or only)
  63. ** document printed is preserved in a static variable.  This is so that the
  64. ** job dialog information can be passed on to documents 2-N in the print job.
  65. ** Calling this function with the document handle nil tells this function
  66. ** that you are done printing documents, and that the print record for the
  67. ** first job can be disposed of. */
  68.  
  69. #pragma segment Print
  70. OSErr    AppPrintDocument(FileRecHndl frHndl, Boolean jobDlg, Boolean firstJob)
  71. {
  72.     OSErr            err;
  73.     THPrint            prRecHndl;
  74.     TPPrPort        printPort;
  75.     GrafPtr            oldPort;
  76.     short            i, keepResFile, copies, fstPage, lstPage;
  77.     TPrStatus        status;
  78.     ControlHandle    proceedButton;
  79.     Rect             rct;
  80.     PrIdleUPP        pidleupp;
  81.  
  82.     static THPrint    prMergeHndl;
  83.  
  84.     if (!frHndl) {
  85.         if (prMergeHndl) {
  86.             DisposeHandle((Handle)prMergeHndl);
  87.             prMergeHndl = nil;
  88.         }
  89.         return(noErr);
  90.     }
  91.  
  92.     PrintingStatusDialog = nil;
  93.  
  94.     if (!(prRecHndl = (THPrint)NewHandle(sizeof(TPrint)))) return(memFullErr);
  95.         /* If we can't generate a print record handle, we are out of here. */
  96.  
  97.     BlockMove((Ptr)&((*frHndl)->doc.print), (Ptr)(*prRecHndl), sizeof(TPrint));
  98.         /* Get the document's print info into the print record handle. */
  99.  
  100.     GetPort(&oldPort);
  101.  
  102.     DoSetCursor(&qd.arrow);
  103.     PrOpen();
  104.     err = PrError();
  105.  
  106.     if (!err) {
  107.         keepResFile = CurResFile();
  108.  
  109.         if (!(*frHndl)->doc.printRecValid) {
  110.             PrintDefault(prRecHndl);            /* The document print record was never 
  111.             err = PrError();                    ** initialized.  Now is is. */
  112.         }            
  113.         if (!err) {
  114.             PrValidate(prRecHndl);        /* Do this just 'cause Apple says so. */
  115.             err = PrError();
  116.         }
  117.         if (!err) {
  118.             if (jobDlg) {                /* User gets to click some buttons. */
  119.                 if (!(PrJobDialog(prRecHndl))) err = userCanceledErr;
  120.                 else                           err = PrError();
  121.             }
  122.         }
  123.         if (!err) {
  124.             if (!firstJob) {
  125.                 fstPage = (*prMergeHndl)->prJob.iFstPage;
  126.                 lstPage = (*prMergeHndl)->prJob.iLstPage;
  127.                 PrJobMerge(prMergeHndl, prRecHndl);
  128.                 (*prMergeHndl)->prJob.iFstPage = (*prRecHndl)->prJob.iFstPage = fstPage;
  129.                 (*prMergeHndl)->prJob.iLstPage = (*prRecHndl)->prJob.iLstPage = lstPage;
  130.                 err = PrError();
  131.             }
  132.         }
  133.  
  134.         if (!err) {            /* Put the defaulted/validated/jobDlg'ed print record in the doc. */
  135.             pidleupp = nil;
  136.             fstPage = (*prRecHndl)->prJob.iFstPage;
  137.             lstPage = (*prRecHndl)->prJob.iLstPage;
  138.             copies  = (*prRecHndl)->prJob.iCopies;
  139.             BlockMove((Ptr)(*prRecHndl), (Ptr)&((*frHndl)->doc.print), sizeof(TPrint));
  140.             (*frHndl)->doc.printRecValid = true;
  141.  
  142.             ParamText((*frHndl)->fileState.fss.name, nil, nil, nil);
  143.             PrintingStatusDialog = GetNewDialog(rPrStatusDlg, nil, (WindowPtr)-1);
  144.             if (PrintingStatusDialog) {
  145.                 GetDialogItem(PrintingStatusDialog, 1, &i, (Handle *)&proceedButton, &rct);
  146.                 HiliteControl(proceedButton, 255);
  147.                     /* Setup the proceed/pause/cancel dialog with the document name. */
  148.                 pidleupp = NewPrIdleProc(PrintIdleProc);
  149.                 (*prRecHndl)->prJob.pIdleProc = pidleupp;
  150.                 UseResFile(keepResFile);
  151.                     /* Hook in the proceed/pause/cancel dialog. */
  152.             }
  153.  
  154.             for (i = 1; (i <= copies) && (!err); ++i) {
  155.  
  156.                 printPort = PrOpenDoc(prRecHndl, nil, nil);
  157.                 if (!(err = PrError())) {
  158.  
  159.                     gPrintPage = 1;
  160.                     while (gPrintPage <= lstPage) {
  161.  
  162.                         PrOpenPage(printPort, nil);
  163.  
  164.                         if (!(err = PrError()))
  165.                             ImageDocument(frHndl, false);
  166.                                 /* Do the print thing here. */
  167.  
  168.                         PrClosePage(printPort);
  169.  
  170.                         if (!gPrintPage) break;
  171.                         ++gPrintPage;
  172.                     }
  173.                     gPrintPage = 0;
  174.                     PrCloseDoc(printPort);
  175.                 }
  176.             }
  177.             if (pidleupp) DisposeRoutineDescriptor(pidleupp);
  178.         }
  179.  
  180.         if (
  181.             (!err) &&
  182.             ((*prRecHndl)->prJob.bJDocLoop == bSpoolLoop) &&
  183.             (!(err = PrError()))
  184.         ) {
  185.             PrPicFile(prRecHndl, nil, nil, nil, &status);
  186.             err = PrError();
  187.         }
  188.     }
  189.  
  190.     if (firstJob) prMergeHndl = prRecHndl;
  191.     else          DisposeHandle((Handle)prRecHndl);
  192.  
  193.     if (PrintingStatusDialog) DisposeDialog(PrintingStatusDialog);
  194.  
  195.     PrClose();
  196.     SetPort(oldPort);
  197.  
  198.     return(err);
  199. }
  200.  
  201.  
  202.  
  203. /*****************************************************************************/
  204.  
  205.  
  206.  
  207. /* PrintIdleProc will handle events in the 'Printing Status Dialog' which
  208. ** gives the user the option to 'Proceed', 'Pause', or 'Cancel' the current
  209. ** printing job during print time.
  210. **
  211. ** The buttons:
  212. **        1: Proceed
  213. **        2: Pause
  214. **        3: Cancel  */
  215.  
  216. #pragma segment Print
  217. pascal void        PrintIdleProc(void)
  218. {
  219.     Boolean                button, paused;
  220.     ControlHandle        pauseButton, proceedButton;
  221.     DialogPtr            aDialog;
  222.     EventRecord            anEvent;
  223.     GrafPtr                oldPort;
  224.     Rect                 rct;
  225.     short                item, itemType, keepResFile;
  226.  
  227.     GetPort(&oldPort);
  228.  
  229.     UseResFile(keepResFile = CurResFile());
  230.  
  231.     GetDialogItem(PrintingStatusDialog, 1, &itemType, (Handle *)&proceedButton, &rct);
  232.     HiliteControl(proceedButton, 255);
  233.     GetDialogItem(PrintingStatusDialog, 2, &itemType, (Handle *)&pauseButton, &rct);
  234.  
  235.     paused = false;
  236.     do {
  237.         if (GetNextEvent((mDownMask + mUpMask + updateMask), &anEvent)) {
  238.             if (PrintingStatusDialog != FrontWindow ())
  239.             SelectWindow(PrintingStatusDialog);
  240.  
  241.             if (IsDialogEvent(&anEvent)) {
  242.                 button = DialogSelect(&anEvent, &aDialog, &item);
  243.  
  244.                 if ((button) && (aDialog == PrintingStatusDialog)) {
  245.                     switch (item) {
  246.                         case 1:
  247.                             HiliteControl(pauseButton, 0);        /* Enable PAUSE    */
  248.                             HiliteControl(proceedButton, 255);    /* Disable PROCEED */
  249.                             paused = false;
  250.                             break;
  251.                         case 2:
  252.                             HiliteControl(pauseButton, 255);    /* Disable PAUSE  */
  253.                             HiliteControl(proceedButton, 0);    /* Enable PROCEED */
  254.                             paused = true;
  255.                             break;
  256.                         case 3:
  257.                             PrSetError(iPrAbort);               /* CANCEL printing */
  258.                             paused = false;
  259.                             break;
  260.                     }
  261.                 }
  262.             }
  263.         }
  264.     } while (paused != false); 
  265.  
  266.     SetPort(oldPort);
  267. }
  268.  
  269.  
  270.  
  271. /*****************************************************************************/
  272.  
  273.  
  274.  
  275. #pragma segment Print
  276. OSErr    PresentStyleDialog(FileRecHndl frHndl)
  277. {
  278.     OSErr        err;
  279.     THPrint        prRecHndl;
  280.  
  281.     if (!(prRecHndl = (THPrint)NewHandle(sizeof(TPrint))))
  282.         return(memFullErr);
  283.  
  284.     PrOpen();
  285.  
  286.     if (!(err = PrError())) {
  287.  
  288.         BlockMove((Ptr)&(*frHndl)->doc.print, (Ptr)*prRecHndl, sizeof(TPrint));
  289.             /* Get data, valid or not. */
  290.  
  291.         if (!(*frHndl)->doc.printRecValid) PrintDefault(prRecHndl);
  292.         else                                PrValidate(prRecHndl);
  293.         if (!(err = PrError())) {
  294.             if (PrStlDialog(prRecHndl)) {
  295.                 BlockMove((Ptr)*prRecHndl, (Ptr)&(*frHndl)->doc.print, sizeof(TPrint));
  296.                 (*frHndl)->doc.printRecValid  = true;
  297.                 (*frHndl)->fileState.docDirty = true;
  298.             }
  299.             else err = userCanceledErr;
  300.         }
  301.     }
  302.  
  303.     DisposeHandle((Handle)prRecHndl);
  304.     PrClose();
  305.  
  306.     return(err);
  307. }
  308.  
  309.  
  310.  
  311.